package com.llkj.qingnianguoxue.mine.mycoin;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.llkj.qingnianguoxue.R;
import com.llkj.qingnianguoxue.adapter.CoinExchangeAdapter;
import com.llkj.qingnianguoxue.base.BaseActivity;
import com.llkj.qingnianguoxue.bean.BookVideoBean;
import com.llkj.qingnianguoxue.bean.BookVideoListBean;
import com.llkj.qingnianguoxue.bean.CoinExchangeBean;
import com.llkj.qingnianguoxue.course.CourseDetailActivity;
import com.llkj.qingnianguoxue.course.CourseTearcherActivity;
import com.llkj.qingnianguoxue.home.ActionDetailActivity;
import com.llkj.qingnianguoxue.home.BookDetailActivity;
import com.llkj.qingnianguoxue.home.JingXuanActivity;
import com.llkj.qingnianguoxue.home.JingXuanBooksActivity;
import com.llkj.qingnianguoxue.home.MasterActivity;
import com.llkj.qingnianguoxue.home.MasterDetailActivity;
import com.llkj.qingnianguoxue.home.PreviousActionActivity;
import com.llkj.qingnianguoxue.home.VideoDetailActivity;
import com.llkj.qingnianguoxue.http.HttpMethod;
import com.llkj.qingnianguoxue.util.GsonUtil;
import com.llkj.qingnianguoxue.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class CoinExchangeActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener,CoinExchangeAdapter.ExchangeItemClick {

    @BindView(R.id.refresh_layout)
    SwipeRefreshLayout refreshLayout;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private CoinExchangeAdapter mExchangeAdapter;
    private List<CoinExchangeBean> mExchangeData;
    private String interal;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_coin_exchange);
        setTitle("范儿币兑换", true, TITLE_TYPE_IMG, R.drawable.icon_back, false, -1,-1);
        ButterKnife.bind(this);
        Intent intent = getIntent();
        interal = intent.getStringExtra("integral");

        initView();

        loadWaitDialog();
        onRefresh();
    }

    private void initView(){
        initStatusbar(TSATUSBAR_TYPE_BLACK_TEXT,rlTitle,R.color.gray_bar);
        refreshLayout.setColorSchemeResources(R.color.blue3989ff);
        recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));

        mExchangeData = new ArrayList<>();
        mExchangeAdapter = new CoinExchangeAdapter(R.layout.item_coin_exchange,mExchangeData,this);
        recyclerView.setAdapter(mExchangeAdapter);

        refreshLayout.setOnRefreshListener(this);
        recyclerView.addOnItemTouchListener(new OnItemChildClickListener() {
            @Override
            public void onSimpleItemChildClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {

                if(view.getId()== R.id.tv_more){
                    CoinExchangeBean exchangeBean = mExchangeData.get(i);
                    int type = exchangeBean.getCategoryType();
                    switch (type){
                        case 1://视频
                            startActivity(JingXuanActivity.class);
                            break;
                        case 2://书籍
                            startActivity(JingXuanBooksActivity.class);
                            break;
                        case 3://导师
                            startActivity(MasterActivity.class);
                            break;
                        case 4://课程
                            startActivity(CourseTearcherActivity.class);
                            break;
                        case 5://活动
                            startActivity(PreviousActionActivity.class);
                            break;
                    }
                }
            }
        });
    }

    @Override
    public void exchangeClick(int type, BookVideoBean item) {
        if(StringUtils.isEmpty(item.getId())){
            return;
        }
        Bundle bundle = null;
        switch (type){
            case 1://视频
                bundle = new Bundle();
                bundle.putString("id", item.getId());
                startActivity(VideoDetailActivity.class, bundle);
                break;
            case 2://书籍
                bundle = new Bundle();
                bundle.putString("id", item.getId());
                startActivity(BookDetailActivity.class, bundle);
                break;
            case 3://导师
                bundle = new Bundle();
                bundle.putString("id",item.getId());
                startActivity(MasterDetailActivity.class, bundle);
                break;
            case 4://课程
                bundle = new Bundle();
                bundle.putString("title",item.getName());
                bundle.putString("id", item.getId());
                startActivity(CourseDetailActivity.class,bundle);
                break;
            case 5://活动
                bundle = new Bundle();
                bundle.putInt("from_class", 1);
                bundle.putString("id", item.getId());
                startActivity(ActionDetailActivity.class,bundle);
                break;
        }
    }

    @Override
    public void onRefresh() {
        HttpMethod.exchangeInteral(this,this,interal);
    }


    @Override
    protected void onhttpSuccess(String data, int method) {
        super.onhttpSuccess(data, method);
        refreshLayout.setRefreshing(false);
        if (method == HttpMethod.HTTP_INDEX_EXCHANGE) {
            BookVideoListBean dataBean = GsonUtil.GsonToBean(data,BookVideoListBean.class);
            if(dataBean!=null){
                mExchangeData.clear();

                CoinExchangeBean exchangeBean = null;
                if(dataBean.getVideo()!=null && dataBean.getVideo().size()>0){
                    exchangeBean = new CoinExchangeBean();
                    mExchangeData.add(exchangeBean);
                    exchangeBean.setCategoryName("范儿视频");
                    exchangeBean.setCategoryType(1);
                    exchangeBean.setCategoryList(dataBean.getVideo());
                }

                if(dataBean.getBooks()!=null && dataBean.getBooks().size()>0){
                    exchangeBean = new CoinExchangeBean();
                    mExchangeData.add(exchangeBean);
                    exchangeBean.setCategoryName("范儿书籍");
                    exchangeBean.setCategoryType(2);
                    exchangeBean.setCategoryList(dataBean.getBooks());
                }


                if(dataBean.getArtist()!=null && dataBean.getArtist().size()>0){
                    exchangeBean = new CoinExchangeBean();
                    mExchangeData.add(exchangeBean);
                    exchangeBean.setCategoryName("范儿导师");
                    exchangeBean.setCategoryType(3);
                    exchangeBean.setCategoryList(dataBean.getArtist());
                }


                if(dataBean.getCurriculum()!=null && dataBean.getCurriculum().size()>0){
                    exchangeBean = new CoinExchangeBean();
                    mExchangeData.add(exchangeBean);
                    exchangeBean.setCategoryName("一对一课程");
                    exchangeBean.setCategoryType(4);
                    exchangeBean.setCategoryList(dataBean.getCurriculum());
                }


                if(dataBean.getLive()!=null && dataBean.getLive().size()>0){
                    exchangeBean = new CoinExchangeBean();
                    mExchangeData.add(exchangeBean);
                    exchangeBean.setCategoryName("范儿活动");
                    exchangeBean.setCategoryType(5);
                    exchangeBean.setCategoryList(dataBean.getLive());
                }

                mExchangeAdapter.notifyDataSetChanged();
            }
        }
    }
}
